package se.webbzon.boltzmann.game.object.trigger;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;

public abstract class AbstractTrigger extends WorldObject {
		
	// The abstract environment associated with this trigger
	private final AbstractEnvironment env;
	
	// The trigger condition
	private TriggerCondition condition; 
	
	// Whatever the trigger condition "create" has been launched
	private boolean conditionCreate;

	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new abstract trigger with a given trigger condition. **/
	public AbstractTrigger(AbstractEnvironment env, TriggerCondition condition) {
		this(0,env,condition);
	}
	
	/** Creates a new abstract trigger with a given trigger condition and
	 * a given set of registered methods. **/
	public AbstractTrigger(	int registeredMethods, AbstractEnvironment env, 
							TriggerCondition condition) {
		super(registeredMethods | condition.getMethod());
		this.env = env;
		this.condition = condition;
	}

	/*============================================================
	Public Methods
	============================================================*/

	/** Returns the trigger condition of this trigger. **/
	public TriggerCondition getCondition() {
		return condition;
	}

	/** Sets the trigger condition of this trigger. **/
	public void setCondition(TriggerCondition condition) {
		this.condition = condition;
	}
	
	/** Returns the abstract environment associated with this 
	 * trigger. **/
	public AbstractEnvironment getEnvironment() {
		return env;
	}
	
	@Override public void onCreate(WorldArea world) {
		conditionCreate = false;
	}

	@Override public void onStepBegin(WorldArea world) {
		if (!env.isDebugMode() && !conditionCreate) {
			trigger();
			conditionCreate = true;
		}
	}

	@Override public void onDestroy(WorldArea world) {
		if (!env.isDebugMode())
			trigger();
	}

	@Override public void onAreaEnter(WorldArea world) {
		if (!env.isDebugMode())
			trigger();
	}

	@Override public void onAreaLeave(WorldArea world) {
		if (!env.isDebugMode())
			trigger();
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	/** This method is called then the trigger is activated. **/
	protected abstract void trigger();
	

}
